package _09Map加强练习;

import java.util.Comparator;
import java.util.HashMap;
import java.util.Set;
import java.util.TreeMap;

/**
 *
 需求：字符串"aababcabcdabcde"
 请统计字符串中每一个字符出现的次数，并按照以下格式输出
 输出结果：
 a（5）b（4）c（3）d（2）e（1）

 */
public class Demo {
    public static void main(String[] args) {
        String s = "aababcabcdabcde";
        TreeMap<Character, Integer> map = new TreeMap<>(new Comparator<Character>() {
            @Override
            public int compare(Character o1, Character o2) {
                return o1-o2;
            }
        });
        for (int i = 0; i < s.length(); i++) {
            char key = s.charAt(i);
            if (map.containsKey(key)){
                Integer value = map.get(key);
                map.put(key,value+1);
            }else{
                map.put(key,1);
            }
        }
        Set<Character> characters = map.keySet();
        for (Character character : characters) {
            System.out.print(character+" ("+map.get(character)+") ");
        }
    }
}
